package niuke.tengxun2021;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/03/21 21:14
 */
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();//数组个数
        int[][] nums = new int[n][];
        String ppp = sc.nextLine();
        int index = 0;
        while (n>0) {
            String num = sc.nextLine();
            num = num.trim();
            String[] strs = num.split(" ");
            int[] nowInts = new int[strs.length - 1];
            for (int i = 1; i < strs.length; i++) {
                nowInts[i - 1] = Integer.valueOf(strs[i]);
            }
            nums[index] = nowInts;
            index++;
            n--;
        }
//        String num = sc.nextLine();
        int guize = sc.nextInt();
        while (guize>0) {
            int indexNum = sc.nextInt();
            PriorityQueue<Integer> small = new PriorityQueue<Integer>(new Comparator<Integer>() {
                public int compare(Integer num1, Integer num2) {
                    return num1.compareTo(num2);
                }
            });
            while (indexNum-->0) {
                int intsIndex = sc.nextInt();
                int[] now = nums[intsIndex - 1];
                for (int i:now){
                    small.add(i);
                }
            }
            int k = sc.nextInt();
            while (k>0){
                System.out.println(small.poll());
                k--;
            }
            guize--;
        }
    }
}
